//
//  NSString+EliminateZero.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSString+EliminateZero.h"
#import "NSDate+Utilities.h"
@implementation NSString (EliminateZero)

- (NSString *)eliminateZeroWithDouble:(NSInteger)integer{
    
    NSString *str = [self copy];
    
    double fdouble = [str doubleValue];
    
    NSString *ftotal;
    switch (integer) {
        case 1:
            ftotal = [NSString stringWithFormat:@"%.1f", fdouble];
            break;
        case 2:
            ftotal = [NSString stringWithFormat:@"%.2f", fdouble];
            break;
        case 3:
            ftotal = [NSString stringWithFormat:@"%.3f", fdouble];
            break;
        case 4:
            ftotal = [NSString stringWithFormat:@"%.4f", fdouble];
            break;
        case 5:
            ftotal = [NSString stringWithFormat:@"%.5f", fdouble];
            break;
        default:
            break;
    }
    
    while ([ftotal hasSuffix:@"0"]) {
        ftotal = [ftotal substringToIndex:[ftotal length]-1];
    }
    
    if ([ftotal hasSuffix:@"."]) {
        ftotal = [ftotal substringToIndex:[ftotal length]-1];
    }
    
    return ftotal;
    
}

+ (NSString *)getTimeDifferentWithNowWithTime:(NSString *)createTime{
    if (createTime.length == 0)//后台没有返回
    {
        return @" ";
    }
    //安全校验字符串（后台传进来的时间戳是13位）
    if ([createTime isKindOfClass:[NSNumber class]]) {
        createTime = [NSString stringWithFormat:@"%lld",[createTime longLongValue]];
    }
    // 获取当前时间戳
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    //截取字符串
    NSUInteger tt = [[createTime substringToIndex:createTime.length - 3] integerValue];
    // 时间差
    NSTimeInterval time = (currentTime - tt);
    //秒
    NSInteger seconds = time;
    if (seconds < 60)
    {
        if (seconds <= 0)
        {
            //有时候会有时间的误差
            seconds = 1;
        }
        return [NSString stringWithFormat:@"%ld秒前",(long)seconds];
    }
    //秒转分钟
    NSInteger minute = time/60;
    if (minute < 60)
    {
        return [NSString stringWithFormat:@"%ld%@",(long)minute,@"分钟前"];
    }
    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld%@",(long)hours,@"小时前"];
    }
    //秒转天数 小于 7天的才告知多少天前
    NSInteger days = time/3600/24;
    if (days < 7)
    {
        return [NSString stringWithFormat:@"%ld%@",(long)days,@"天前"];
    }
    
    //格式化日期对象格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    //将要返回的字符串
    NSString *confromTimespStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:tt]];
    return confromTimespStr;
}
+ (NSString*) getDateTimeWithTimeSpace:(long long)createTime{
    NSDate *timedate = [NSDate dateWithTimeIntervalSince1970:createTime/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *confromTimespStr = nil;
    NSString *preString = @"";
    if (timedate.isToday) {  //当天
         [formatter setDateFormat:@"HH:mm"];
         return [formatter stringFromDate:timedate];
    }else if (timedate.isYesterday){ //昨天
         [formatter setDateFormat:@"HH:mm"];
         preString = @"昨天 ";
    }else if (timedate.isContain7day){ //本周
         [formatter setDateFormat:@"HH:mm"];
         preString = timedate.weekDay;
    }else{ //超过本周
         preString = @"";
         [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    confromTimespStr = [formatter stringFromDate:timedate];
    return [NSString stringWithFormat:@"%@%@",preString,confromTimespStr];
}
/**
 专门为IM提供时间段服务
 @param createTime 时间戳
 @return 返回类似 时-分-秒
 */
+ (NSString*)getDateForIMTimeWithTimeSpace:(long long)createTime{
    NSDate *timedate = [NSDate dateWithTimeIntervalSince1970:createTime/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSString *confromTimespStr = nil;
    NSString *preString = @"";
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval seconds = currentTime - createTime/1000;
    NSInteger minute = seconds/60;
    if (timedate.isToday) {
        // 获取当前时间戳
        if (seconds < 60){//小于60秒
            return  @"刚刚";
        }if (minute < 60){// 小于60分钟
            return [NSString stringWithFormat:@"%ld%@",(long)minute,@"分钟前"];
        }
        [formatter setDateFormat:@"HH:mm"];
        return [formatter stringFromDate:timedate];
    }else if (timedate.isYesterday){ //昨天
         [formatter setDateFormat:@"HH:mm"];
         preString = @"昨天 ";
    }else if (seconds <= (24 * 60 * 60 * 30)){ //两天到30天  24 *60*60 *30
        [formatter setDateFormat:@"MM-dd HH:mm"];
         return [formatter stringFromDate:timedate];
    }else if (timedate.isThisYear){//同一个年份
        [formatter setDateFormat:@"MM-dd"];
        return [formatter stringFromDate:timedate];
    }else{ //超过一年
        preString = @"";
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    confromTimespStr = [formatter stringFromDate:timedate];
    return [NSString stringWithFormat:@"%@%@",preString,confromTimespStr];
}
+ (NSString *)getYYMMDDTimeWithTimeSpace:(NSInteger)createtime{
      NSDate *timedate = [NSDate dateWithTimeIntervalSince1970:createtime/1000];
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      NSString *confromTimespStr = nil;
      [formatter setDateFormat:@"yyyy.MM.dd HH:mm"];
      confromTimespStr = [formatter stringFromDate:timedate];
      return [NSString stringWithFormat:@"%@",confromTimespStr];
    
}

//对含有emoji表情的字符串做处理
-(NSInteger)emoji_lengthWith
{
    __block NSInteger length = 0;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
        length++;
    }];
    return length;;
}

@end
